package com.example.javabasic.thread.classthree.waitsync;

import com.example.javabasic.thread.classone.demo4.Test;

/**
 * @description  测试wait
 *
 * @Author Lin FuYuan
 * @date 2021/7/16
 */
public class TestWait {

    Object obj = new Object();

    public static void main(String[] args) {
        TestWait testWait = new TestWait();
        new Thread(()->{
            try {
                testWait.test();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"线程1").start();
        new Thread(()->{
            try {
                testWait.test();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"线程2").start();

    }
    public void test() throws InterruptedException {
        System.out.println(Thread.currentThread().getName()+  "   test sync 代码块前");
        synchronized (obj){
            System.out.println(Thread.currentThread().getName()+  "        进入test sync代码块");
            Thread.sleep(3000);
            try {
                obj.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            System.out.println(Thread.currentThread().getName()+  "     test sync obj wait 结束");
        }
    }
}
